﻿using System;
using System.Collections.Generic;
using System.Management;
using System.Security.Cryptography;
using System.Text;

namespace OlivingRegister
{



    public class Register
    {
        public static string[] _DigitToChar = new string[] { "+", "-", "=", "!", "#", ")", "/", "@", "$", "|" };


        #region private



        private static string Decrypt(string original)
        {
            byte[] inputBuffer = Convert.FromBase64String(original);
            byte[] bytes = Encoding.Default.GetBytes("CYT");
            TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider
            {
                Key = MakeMD5(bytes),
                Mode = CipherMode.ECB
            };
            return Encoding.Default.GetString(provider.CreateDecryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length));
        }

        private static string Encrypt(string original)
        {
            byte[] bytes = Encoding.Default.GetBytes(original);
            byte[] buffer2 = Encoding.Default.GetBytes("CYT");
            TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider
            {
                Key = MakeMD5(buffer2),
                Mode = CipherMode.ECB
            };
            return Convert.ToBase64String(provider.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length));
        }

        private static string getCpu()
        {
            ManagementClass class2 = new ManagementClass("win32_Processor");
            foreach (ManagementObject obj2 in class2.GetInstances())
            {
                return obj2.Properties["Processorid"].Value.ToString();
            }
            return null;
        }

        private static string GetDiskVolumeSerialNumber()
        {
            new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObject obj2 = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
            obj2.Get();
            return obj2.GetPropertyValue("VolumeSerialNumber").ToString();
        }

        private static string GetRegisterCode(string sMachineCode) =>
            Encrypt(sMachineCode).ToUpper();

        private static byte[] MakeMD5(byte[] original)
        {
            byte[] buffer = new MD5CryptoServiceProvider().ComputeHash(original);
            return buffer;
        }



        #endregion




        /// <summary>
        /// 读取机器码
        /// </summary>
        /// <returns></returns>
        public static string ReadMachineCode() => (getCpu() + GetDiskVolumeSerialNumber()).Substring(0, 0x18);
         
      




        /// <summary>
        ///  验证 注册码
        /// </summary>
        /// <param name="sMachineCode"></param>
        /// <param name="ReturnRegisterCode"></param>
        /// <param name="dtTime"></param>
        /// <returns></returns>
        public static bool VerifyRegisterCode(string sMachineCode, string ReturnRegisterCode, out DateTime dtTime)
        {
            string registerCode = GetRegisterCode(sMachineCode);
            string str2 = AnalysisRegisterCode(ReturnRegisterCode, out dtTime);
            return (registerCode == str2);
        }


        private static string AnalysisRegisterCode(string ReturnRegisterCode, out DateTime dtTime)
        {
            string str = string.Empty;
            string str2 = $"20{Array.IndexOf<string>(_DigitToChar, ReturnRegisterCode.Substring(4, 1))}{Array.IndexOf<string>(_DigitToChar, ReturnRegisterCode.Substring(0, 1))}-{Array.IndexOf<string>(_DigitToChar, ReturnRegisterCode.Substring(7, 1))}{Array.IndexOf<string>(_DigitToChar, ReturnRegisterCode.Substring(12, 1))}-{Array.IndexOf<string>(_DigitToChar, ReturnRegisterCode.Substring(13, 1))}{Array.IndexOf<string>(_DigitToChar, ReturnRegisterCode.Substring(ReturnRegisterCode.Length - 1, 1))}";


            str = ReturnRegisterCode.Remove(ReturnRegisterCode.Length - 1, 1).Remove(13, 1).Remove(12, 1).Remove(7, 1).Remove(4, 1).Remove(0, 1);
            dtTime = Convert.ToDateTime(str2);
            return str;
        }

    }



}
